package com.android.core.widget.flashview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aidabaobei.core.R;
import com.android.core.utils.glide.GlideImgManager;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：hewk0.1
 * 类描述：轮播图view
 * 创建人：lh
 * 创建时间：2015/11/3 15:21
 * 修改人：lh
 * 修改时间：2015/11/3 15:21
 * 修改备注：
 *
 * @version v1.0
 */
public class FlashView extends FrameLayout implements GestureDetector.OnGestureListener {
    private ImageHandler mhandler = new ImageHandler(new WeakReference<>(this));
    private List<String> imageUris;
    private List<ImageView> imageViewsList;
    private List<ImageView> dotViewsList;// 点的
    private LinearLayout mLinearLayout;// 点的线性布局
    private ViewPager mViewPager;
    private FlashViewListener mFlashViewListener;// 向外提供接口
    private int dot1 = R.mipmap.ic_flashview_tiny_line_checked;// 选中点的 当前图片
    private int dot2 = R.mipmap.ic_flashview_tiny_line_default;// 没选中点
    //    private int dot1 = R.mipmap.ic_launcher;// 没选中点
//    private int dot2 = R.mipmap.ic_launcher;// 选中点的 当前图片
    private int dotMargin = 25;// 点之间的间隙 单位 是px
    private MyPagerAdapter adapter;
    private LinearLayout.LayoutParams lp;
    private GestureDetector mGestureDetector;// 增加长按停止轮播的手势检测

    private Context context;

    public FlashView(Context context) {
        this(context, null);
    }

    public FlashView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlashView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        initUI(context);
        if (!(imageUris.size() <= 0)) {
            setImageUris(imageUris);
        }

    }

    private void initUI(Context context) {
        imageViewsList = new ArrayList<ImageView>();
        dotViewsList = new ArrayList<ImageView>();
        imageUris = new ArrayList<String>();
        mGestureDetector = new GestureDetector(context, this);
        LayoutInflater.from(context).inflate(R.layout.flashview_layout, this,
                true);

        mLinearLayout = (LinearLayout) findViewById(R.id.ll_dot);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);

    }

    public void setImageUris(List<String> imageuris) {
        imageUris.clear();
        imageViewsList.clear();
        dotViewsList.clear();
        mLinearLayout.removeAllViews();
        if (null == imageuris || imageuris.size() <= 0) {// 如果得到的图片张数为0，则增加一张默认的图片
            imageUris.add("mipmap://" + R.mipmap.em_empty_photo);
        } else {
            for (int i = 0; i < imageuris.size(); i++) {
                imageUris.add(imageuris.get(i));
            }
        }
        if (lp == null) {
            lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(dotMargin, 0, 0, 0);
        }
        int currentPosition = 0;
        if (mViewPager.getCurrentItem() > 0) {
            currentPosition = (mViewPager.getCurrentItem() - 100 * imageuris.size()) % (imageuris.size());
        }
        for (int i = 0; i < imageUris.size(); i++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);// X和Y方向都填满
            GlideImgManager.loadImage(context,imageuris.get(i),imageView);
            imageViewsList.add(imageView);
            ImageView viewDot = new ImageView(getContext());
            if (i == currentPosition) {
                viewDot.setBackgroundResource(dot1);
            } else {
                viewDot.setBackgroundResource(dot2);
            }
            viewDot.setLayoutParams(lp);
            dotViewsList.add(viewDot);
            mLinearLayout.addView(viewDot);
        }
        mViewPager.setFocusable(true);
        if (adapter == null) {
            adapter = new MyPagerAdapter();
            mViewPager.setAdapter(adapter);
            mViewPager.setOnPageChangeListener(new MyPageChangeListener());
            if (imageUris.size() <= 1) {

            } else {
                mhandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE,
                        ImageHandler.MSG_DELAY);
            }
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 切换轮播小点的显示
     *
     * @param selectItems
     */
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < dotViewsList.size(); i++) {
            if (i == selectItems % dotViewsList.size()) {
                dotViewsList.get(i).setBackgroundResource(dot1);
            } else {
                dotViewsList.get(i).setBackgroundResource(dot2);
            }
        }
    }

    /**
     * 数据适配器
     */
    private class MyPagerAdapter extends PagerAdapter {

        private static final int FAKE_BANNER_SIZE = 50;


        @Override
        public int getCount() {
            return imageViewsList.size() <= 1 ? 1 : FAKE_BANNER_SIZE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            final int pos = position;
            position %= imageViewsList.size();
            if (position < 0) {
                position = imageViewsList.size() + position;
            }
            View view = imageViewsList.get(position);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mFlashViewListener.onClickImage(pos);
                }
            });
            //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
            ViewParent vp = view.getParent();
            if (vp != null) {
                ViewGroup parent = (ViewGroup) vp;
                parent.removeView(view);
            }
            container.addView(view);
            //add listeners here if necessary
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            int position = mViewPager.getCurrentItem();
            if (position == 0) {
                position = imageViewsList.size();
                mViewPager.setCurrentItem(position, true);
            } else if (position == FAKE_BANNER_SIZE - 1) {
                position = imageViewsList.size() - 1;
                mViewPager.setCurrentItem(position, true);
            }
        }


    }

    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

            switch (arg0) {
                case ViewPager.SCROLL_STATE_DRAGGING:
                    mhandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT);
                    break;
                case ViewPager.SCROLL_STATE_IDLE:
                    mhandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE,
                            ImageHandler.MSG_DELAY);
                    break;
                default:
                    break;
            }

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int pos) {
            // TODO Auto-generated method stub
            mhandler.sendMessage(Message.obtain(mhandler,
                    ImageHandler.MSG_PAGE_CHANGED, pos, 0));
            setImageBackground(pos);

        }

    }

    @SuppressWarnings("unused")
    private void destoryBitmaps() {
        for (int i = 0; i < imageViewsList.size(); i++) {
            ImageView imageView = imageViewsList.get(i);
            Drawable drawable = imageView.getDrawable();
            if (drawable != null) {
                drawable.setCallback(null);
            }
        }
    }

    public void setPageTransformer(ViewPager.PageTransformer rotateTransformer) {
        // TODO Auto-generated method stub
        mViewPager.setPageTransformer(false, rotateTransformer);
    }

    private static class ImageHandler extends Handler {

        protected static final int MSG_UPDATE_IMAGE = 1;

        protected static final int MSG_KEEP_SILENT = 2;

        protected static final int MSG_BREAK_SILENT = 3;

        protected static final int MSG_PAGE_CHANGED = 4;

        protected static final long MSG_DELAY = 4000;

        private WeakReference<FlashView> weakReference;
        public int currentItem = 0;

        protected ImageHandler(WeakReference<FlashView> wk) {
            weakReference = wk;

        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            FlashView activity = weakReference.get();
            if (activity == null) {
                return;
            }
            if (activity.mhandler.hasMessages(MSG_UPDATE_IMAGE)) {
                if (currentItem > 0)// 这里必须加入currentItem>0的判断，否则不能完美的自动轮播
                {
                    activity.mhandler.removeMessages(MSG_UPDATE_IMAGE);
                }
            }
            switch (msg.what) {
                case MSG_UPDATE_IMAGE:
                    currentItem++;
                    activity.mViewPager.setCurrentItem(currentItem);
                    break;
                case MSG_KEEP_SILENT:
                    break;
                case MSG_BREAK_SILENT:
                    activity.mhandler.sendEmptyMessageDelayed(MSG_UPDATE_IMAGE,
                            MSG_DELAY);
                    break;
                case MSG_PAGE_CHANGED:
                    currentItem = msg.arg1;
                    activity.mhandler.sendEmptyMessageDelayed(MSG_UPDATE_IMAGE,
                            MSG_DELAY);
                    break;
                default:
                    break;
            }
        }

    }

    /**
     * 设置点击图片点击监听
     *
     * @param mListener
     */
    public void setOnFlashViewListener(FlashViewListener mListener) {
        mFlashViewListener = mListener;
    }

    /**
     * 设置点的位置 可以设置左 右 居中
     *
     * @param gravity
     */
    public void setDogGravity(int gravity) {
        mLinearLayout.setGravity(gravity);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_CANCEL
                || ev.getAction() == MotionEvent.ACTION_UP) {
            mhandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, 500);
        }
        mGestureDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        mhandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }
}
